<?php
	include_once("cmsConfig.php");
	
	$pageNumber = extractNumberWithKeyFromArray('pageNumber', $_GET);//will only come in as a get argument

	$formDescription = $formDescriptorTableWriter->readItem($formid);

	$documentType = new CmsTableWriter("documenttype",$db);
	
	if (is_numeric($pageNumber) && $pageNumber > 0 ) {
		$cmsDocument->setPageNumber($pageNumber);
	}
	
	$cmsDocument->setPageSize( CMS_PAGE_LENGTH );

	$cmsDocument->setFormIds(array($formid));
//	$cmsDocument->addSort("<<rank>>");

	$sortColumns = array();
	if (is_string($sorts)) {
		if(!is_string($direction)) {
			$direction = "DESC";
		}
		$sortColumns = explode(",", $sorts);
		foreach($sortColumns as $sort){
			$cmsDocument->addSort($sort, $direction);
		}	
	}
	
	$returnURL = $_SERVER['PHP_SELF'] . "?fileNo=1&formid=" . $formid ."&sorts=clear&direction=clear&pageNumber=$pageNumber";
	$cancelURL = $returnURL;

	$addMode = null;
	$addLabel = null;
	
	//constraint on the group of the mode you are viewing in.
	//$viewMode =  extractNumberWithKeyFromArray('viewMode',$_SESSION, null);
	$viewModeGid = groupForMode($viewMode, $db);
	
	if (CMS_ENABLE_SITE_CONTENT) {	
		$userSites = getSiteModesForUser($authInfo->uid, $db);
		
		$isInSiteMode = false;
		foreach($userSites as $key=>$value){
			if($isInSiteMode = ($value->gid == $viewModeGid)){
				$cmsDocument->addSiteToSiteFilters($value->gid);
				break;
			}
		}
			
		// if we are not in a site mode: display everything
		if(!$isInSiteMode){
			
		//	foreach($userSites as $key=>$value){
		//		$cmsDocument->addSiteToSiteFilters($value->gid);
		//	}
		}
		
	} //else{
		
		if (CMS_ENABLE_USER_PERMISSIONS)
			$cmsDocument->addConstraint("gid", $viewModeGid);
	
	//}	
	

	ob_start();
	if(!key_exists($formid, $CMS_CUSTOM_REPORTS)) { 
		include_once("cmsBrowseContentDefault.php");
	} else {
		include_once($CMS_CUSTOM_REPORTS[$formid]);
	}
	$browsePageContent = ob_get_clean();
	
	$cmsPageNumber = $cmsDocument->pageNumber();
	$cmsTotalPages = $cmsDocument->totalPages();



?>

<div id="cms_browse_heading" class="cms_browse_heading">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td valign="top" align="left">
		    <h1>
		      Browse <?= $formDescription->formname ?>
		      <?
		      if ($formDescription->formid == MAIL_LIST_FORM && isset($_GET["user"]) && $_GET["user"]) {
			      $userTableWriter = new CmsTableWriter("phplist_user_user_attribute", $db);
			      $userTableWriter->resetCustomIterator(" WHERE attributeid=2 AND userid='".$_GET["user"]."'");
			      if ($user = $userTableWriter->nextItem()) {
				      echo " for ".magicClean($user->value);
			      }
		      }
		      elseif ($formDescription->formid == MAIL_LIST_USER_FORM && isset($_GET["list"]) && $_GET["list"]) {
			      $listTableWriter = new CmsTableWriter("phplist_list", $db);
			      $listTableWriter->resetCustomIterator(" WHERE id='".$_GET["list"]."'");
			      if ($list = $listTableWriter->nextItem()) {
				      echo " to ".magicClean($list->name);
			      }
		      }
		      ?>
		    </h1>
		  </td>
                  <td align="right" valign="top"><?
	//if(addFormAuth($authInfo, $formDescription, null, $db)) { 
	if (addFormAuth($authInfo, $formDescription, null, $db)) {
		// determine what add mode to use.. and insert the correct  add button as needed.
		$addInputFields = "";
		switch ($addMode) {
			//add with a folder
			case CMS_ADD_QUICK_ADD_FOLDER:
				displayFolderQuickAdd($formDescription, $addLabel, $authInfo, null, null, $db);
			break;
			
			case CMS_ADD_FILES_AND_FOLDERS:
				displayFilesFoldersQuickAdd($formDescription, $addLabel, $authInfo, null, null, $db);
			break;
			
			case CMS_ADD_QUICK_ADD_WITH_PERMISSIONS_GO:
				if (CMS_ENABLE_USER_PERMISSIONS ) {
					if ($authInfo->usertype != CMS_USERTYPE_SU ) {
						$groups = groupsForUserId($authInfo->uid, $db);
						$q = "SELECT gid, groupname FROM grouplist WHERE gid IN (" . implode(',', $groups ) . ") ORDER BY gid";
					} else {
						$q = "SELECT gid, groupname FROM grouplist  ORDER BY gid";			
					}
					$addInputFields .= "<select name=\"gid\" >\n";
					//get the groups here....
					$db->executeQuery($q);
					echo "<option  value=\"" . 0 . "\">-</option>\n";
					while ($row = $db->nextObject()) {
						if ($row->gid == $formDescription->gid ) {
							$selected = "selected=\"yes\""; 
						} else { 
							$selected = "";
						}
						$addInputFields .=  "<option ". $selected ." value=\"" . $row->gid . "\">" . $row->groupname . "</option>\n";
					}
				
					$addInputFields .=  "</select>\n";
				}				
				
			// add with out
			case CMS_ADD_QUICK_ADD_GO://expects  a dictionary of options to turn into a url.
				if (isset($addUrlDict) && is_array($addUrlDict) && count($addUrlDict)) {
					// serialised the dict into a url....
					$addUrlBase = array();
					foreach ( $addUrlDict as $key => $value) {
					 array_push($addUrlBase ,"<input type=\"hidden\" name=\"" . $key	. "\" value=\"" . urlencode( $value ) . "\" />");
					}
					$addInputFields .= implode("\n", $addUrlBase);
				}
	//		break;
			default:
				if ( !isset($addInputFields) || !$addInputFields){
						$addInputFields = "<input type=\"hidden\" name=\"fileNo\" value=\"2\" />\n";
					
					$addInputFields .= "<input type=\"hidden\" name=\"formId\" value=\"$formDescription->formid\"/>\n";// .
					//"<input type=\"hidden\" name=\"formId\" value=\"$formDescription->formid\"/>";
				}
				
				$self = $_SERVER['PHP_SELF'];
				print <<< EOD
			<form method="post" action="$self">
			$addInputFields
			<input type="image" src="/cms/images/cnt_add_new_record.gif" />
			</form>
EOD;
		}
	}	
?></td>
                </tr>
              </table>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>

<div id="cms_browse">
<?
	//dump the stuff here...
	echo $browsePageContent;
?>
</div>
<div id="cms_browse_pagination">
<?
// more $_REQUEST variables for pagenations if necessary
$extraUrl='';
if(isset($CMS_CUSTOM_PAGENATION_EXTRA_VARIABLES) && is_array($CMS_CUSTOM_PAGENATION_EXTRA_VARIABLES) && array_key_exists($formid, $CMS_CUSTOM_PAGENATION_EXTRA_VARIABLES)){
	$cmsPageNationExtraValue =  extractArrayWithKeyFromArray( $formid, $CMS_CUSTOM_PAGENATION_EXTRA_VARIABLES, array() );

	foreach($cmsPageNationExtraValue as $value){
		$extraUrl.="&$value=${$value}";
	}
}

// display previous
$self = $_SERVER['PHP_SELF'];
if ($pageNumber > 0) {
	$lastPage = $pageNumber-1;
	print <<< EOD
<a href="$self?pageNumber=$lastPage&fileNo=1&formid=$formid$extraUrl"><img src="/cms/images/cnt_previous.gif" width="104" height="24" align="absmiddle"/></a>
EOD;
} else {
	print <<< EOD
<img src="/cms/images/spacer.gif" width="104" height="24"/>
EOD;
}

// display pagination
if($cmsTotalPages>1){
?><span class="grey">&nbsp;
<?
	unset($cmsPage);
	$start = ($cmsPageNumber+1+1-10 > 0)?($cmsPageNumber+1+1-10):1;
	$end = ($cmsPageNumber+10 < $cmsTotalPages)?($cmsPageNumber+10):$cmsTotalPages;
	for($i=$start;$i<=$end;$i++){
		$cmsPage[$i]=($i==$cmsPageNumber+1)?$i:"<a href=\"?fileNo=1&formid=$formid&pageNumber=".($i-1)."$extraUrl\">$i</a>";
	}
	print implode(" - ", $cmsPage);
?>&nbsp;</span>  
<?
}

// display next
if ($cmsPageNumber < $cmsTotalPages - 1) {
	$nextPage = $pageNumber+1;
	print <<< EOD
<a href="$self?pageNumber=$nextPage&fileNo=1&formid=$formid$extraUrl"><img width="106" height="24" src="/cms/images/cnt_next.gif" align="absmiddle"/></a>
EOD;
}
?>
</div>
